﻿namespace WMS6.Web.VAS
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.VASServiceRef;

    public class WorkCenter : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox cheIsActive;
        protected HtmlForm F79741;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnWorkCenterID;
        protected HtmlHead Head1;
        protected HtmlImage img1;
        protected HtmlImage img2;
        protected HtmlImage img3;
        protected HtmlImage imgOwnerID;
        protected Label lblDescr;
        protected Label lblLoc;
        protected Label lblLocFinishedGood;
        protected Label lblLocInbound;
        protected Label lblLocOpComplete;
        protected Label lblMsg;
        protected Label lblOperationID;
        protected Label lblOwner;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescr;
        protected TextBox txtLoc;
        protected TextBox txtLocFinishedGood;
        protected TextBox txtLocInbound;
        protected TextBox txtLocOpComplete;
        protected TextBox txtWorkCneterID;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtWorkCneterID.Text = "";
            this.txtWorkCneterID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtWorkCneterID.Text.Trim() != "")
                {
                    List<string> workCneterIDs = new List<string> {
                        this.txtWorkCneterID.Text.Trim()
                    };
                    this.lblMsg.Text = VASServiceClientProxy.GetInstance().DeleteWorkCenterInfo(workCneterIDs) + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("WorkCenterList.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("WorkCenterList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtWorkCneterID.Text))
            {
                this.lblMsg.Text = "请输入工作中心!";
            }
            else if (string.IsNullOrEmpty(this.txtLocInbound.Text))
            {
                this.lblMsg.Text = "请输入收料货位!";
            }
            else if (string.IsNullOrEmpty(this.txtLoc.Text))
            {
                this.lblMsg.Text = "请输入加工货位!";
            }
            else if (string.IsNullOrEmpty(this.txtLocOpComplete.Text))
            {
                this.lblMsg.Text = "请输入完工货位!";
            }
            else if (string.IsNullOrEmpty(this.txtLocFinishedGood.Text))
            {
                this.lblMsg.Text = "请输入成品落放货位!";
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateWorkCenter();
            }
            else
            {
                this.InsertWorkCenter();
            }
        }

        private WorkCenterInfo GetWorkCenterInfo(WorkCenterInfo workCenterInfo)
        {
            workCenterInfo.WorkCenterID = this.txtWorkCneterID.Text.Trim();
            workCenterInfo.Descr = this.txtDescr.Text.Trim();
            workCenterInfo.Loc = this.txtLoc.Text.Trim();
            workCenterInfo.LocFinishedGood = this.txtLocFinishedGood.Text.Trim();
            workCenterInfo.LocInbound = this.txtLocInbound.Text.Trim();
            workCenterInfo.LocOpComplete = this.txtLocOpComplete.Text.Trim();
            workCenterInfo.IsActive = this.cheIsActive.Checked ? "Y" : "N";
            return workCenterInfo;
        }

        private void InsertWorkCenter()
        {
            try
            {
                WorkCenterInfo workCenterInfo = new WorkCenterInfo();
                workCenterInfo = this.GetWorkCenterInfo(workCenterInfo);
                workCenterInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = VASServiceClientProxy.GetInstance().InsertOperationInfo(workCenterInfo) + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = workCenterInfo;
                this.txtWorkCneterID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnWorkCenterID.Value = base.Request.QueryString["ID"].ToString();
                this.SetWorkCenter2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetWorkCenter2Form()
        {
            try
            {
                string workCenterID = this.hdnWorkCenterID.Value;
                WorkCenterInfo workCenterInfo = VASServiceClientProxy.GetInstance().GetWorkCenterInfoByID(workCenterID);
                base.DataEntityViewState = workCenterInfo;
                if (workCenterInfo != null)
                {
                    this.txtWorkCneterID.Text = workCenterInfo.WorkCenterID;
                    this.txtWorkCneterID.Enabled = false;
                    this.txtLoc.Text = workCenterInfo.Loc;
                    this.txtLocFinishedGood.Text = workCenterInfo.LocFinishedGood;
                    this.txtLocInbound.Text = workCenterInfo.LocInbound;
                    this.txtLocOpComplete.Text = workCenterInfo.LocOpComplete;
                    this.txtDescr.Text = workCenterInfo.Descr;
                    this.cheIsActive.Checked = workCenterInfo.IsActive == "Y";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateWorkCenter()
        {
            try
            {
                WorkCenterInfo workCenterInfo = base.DataEntityViewState as WorkCenterInfo;
                workCenterInfo = this.GetWorkCenterInfo(workCenterInfo);
                workCenterInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = VASServiceClientProxy.GetInstance().UpdateOperationInfo(workCenterInfo) + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

